*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *;   
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;


*Figures 4.8-4.10


set more off


use Elitecom.dta, clear
 

tab country
tab country, nol

*Germany

preserve
reg conf t_in_p if country==1 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(inp_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
*non–integer degrees of freedom for unequal–variance t–tests 
*(Satterthwaite, 1946)[5], which metaparm uses as the default
restore

preserve
reg conf t_in_n if country==1 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(inn_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_p if country==1 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(outp_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_n if country==1 [aw=weight], cluster(id)   
parmest, label norestore escal(N) saving(outn_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

***
preserve
reg conf t_cso_p if country==1 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(csop_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_cso_n if country==1 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(cson_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_p if country==1 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(iop_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_n if country==1 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(ion_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_p if country==1 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(govp_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_n if country==1 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(govn_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore


***

preserve
reg conf t_p if country==1 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(pos_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_n if country==1 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(neg_un.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 



*UK

preserve
reg conf t_in_p if country==2 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(inp_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_in_n if country==2 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(inn_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_p if country==2 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(outp_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_n if country==2 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(outn_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

***
preserve
reg conf t_cso_p if country==2 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(csop_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_cso_n if country==2 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(cson_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_p if country==2 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(iop_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_n if country==2 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(ion_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_p if country==2 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(govp_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_n if country==2 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(govn_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore
 

***

preserve
reg conf t_p if country==2 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(pos_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_n if country==2 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(neg_imf.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore




*US
preserve
reg conf t_in_p if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(inp_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_in_n if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(inn_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_p if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(outp_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_n if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(outn_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore
 
***
preserve
reg conf t_cso_p if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(csop_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_cso_n if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(cson_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_p if country==3 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(iop_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_n if country==3 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(ion_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_p if country==3 [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(govp_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_n if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(govn_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

***
preserve
reg conf t_p if country==3 [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(pos_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_n if country==3 [aw=weight], cluster(id)   
parmest, label norestore escal(N) saving(neg_wto.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore


***Merge data sets for Figures 4-6

 
use inp_un.dta, clear
merge 1:1 parm using inn_un.dta
drop _merge
merge 1:1 parm using outp_un.dta
drop _merge
merge 1:1 parm using outn_un.dta
drop _merge
merge 1:1 parm using csop_un.dta
drop _merge
merge 1:1 parm using cson_un.dta
drop _merge
merge 1:1 parm using iop_un.dta
drop _merge
merge 1:1 parm using ion_un.dta
drop _merge
merge 1:1 parm using govp_un.dta
drop _merge
merge 1:1 parm using govn_un.dta 
drop _merge
merge 1:1 parm using pos_un.dta
drop _merge
merge 1:1 parm using neg_un.dta 
drop _merge
saveold germany.dta, replace

use inp_imf.dta, clear
merge 1:1 parm using inn_imf.dta
drop _merge
merge 1:1 parm using outp_imf.dta
drop _merge
merge 1:1 parm using outn_imf.dta
drop _merge
merge 1:1 parm using csop_imf.dta
drop _merge
merge 1:1 parm using cson_imf.dta
drop _merge
merge 1:1 parm using iop_imf.dta
drop _merge
merge 1:1 parm using ion_imf.dta
drop _merge
merge 1:1 parm using govp_imf.dta
drop _merge
merge 1:1 parm using govn_imf.dta 
drop _merge
merge 1:1 parm using pos_imf.dta
drop _merge
merge 1:1 parm using neg_imf.dta 
drop _merge
saveold uk.dta, replace

use inp_wto.dta, clear
merge 1:1 parm using inn_wto.dta
drop _merge
merge 1:1 parm using outp_wto.dta
drop _merge
merge 1:1 parm using outn_wto.dta
drop _merge
merge 1:1 parm using csop_wto.dta
drop _merge
merge 1:1 parm using cson_wto.dta
drop _merge
merge 1:1 parm using iop_wto.dta
drop _merge
merge 1:1 parm using ion_wto.dta
drop _merge
merge 1:1 parm using govp_wto.dta
drop _merge
merge 1:1 parm using govn_wto.dta 
drop _merge
merge 1:1 parm using pos_wto.dta
drop _merge
merge 1:1 parm using neg_wto.dta 
drop _merge
saveold us.dta, replace

ed

*Merge data in IO-specific datasets first
****************************************************************
use germany.dta, clear
ed
drop in 1
drop label
gen country=1
save germany.dta, replace

use uk.dta, clear
ed
drop in 1
drop label
gen country=2
save uk.dta, replace

use us.dta, clear
ed
drop in 1
drop label
gen country=3
save us.dta, replace



*Append all files
************************************************
*US file is open

append using uk.dta
append using germany.dta

*Input-output - figure 

label define cntry 1 "Germany" 2 "UK" 3 "US"
label values country cntry 

gen tg=.
label var tg "Treatment group"
sort tg country

gen ate=.
gen yl=.
gen yu=.

saveold allcountries.dta, replace

sort parm
ed
replace tg=1 if parm=="t_in_p"
replace tg=2 if parm=="t_in_n"
replace tg=3 if parm=="t_out_p"
replace tg=4 if parm=="t_out_n"

label define tg 1 "Positive-input" 2 "Negative-input" 3 "Positive-output" 4 "Negative-output" 
label values tg tg

drop if tg==.
saveold allcountries-inp-outp.dta, replace

*Elite types - figure 

use allcountries.dta, clear
ed
replace tg=1 if parm=="t_cso_p"
replace tg=2 if parm=="t_cso_n"
replace tg=3 if parm=="t_io_p"
replace tg=4 if parm=="t_io_n"
replace tg=5 if parm=="t_gov_p"
replace tg=6 if parm=="t_gov_n"

label define tg 1 "Positive-CSOs" 2 "Negative-CSOs" 3 "Positive-IOs" 4 "Negative-IOs" 5 "Positive-Governments" 6 "Negative-Governments" 
label values tg tg

drop if tg==.
saveold allcountries-elites.dta, replace

*Valence - figure 6

use allcountries.dta, clear
ed
replace tg=1 if parm=="t_p"
replace tg=2 if parm=="t_n" 

label define tg 1 "Positive" 2 "Negative" 
label values tg tg

drop if tg==.
saveold allcountries-tone.dta, replace


***Figures will look like this:

/*
		|
		|			io
		|
y=ATE	|
		|
		|
		|
		|
		-------------------
				x=tg
				

				*/
				
				
		
***NOW code for figures
*Fig 4.8
************************************************
use allcountries-elites.dta, clear
ed
drop yl yu ate
				
rename min95 yl
rename max95 yu
rename estimate ate
			
local varname ate
local group1 tg
local group2 country

sort `group1' `group2'
gen x = _n
replace x = _n + 1 if _n > 3
replace x = _n + 2 if _n > 6
replace x = _n + 3 if _n > 9 
replace x = _n + 4 if _n > 14
replace x = _n + 5 if _n > 17

separate ate, by(country)
separate yu, by(country)
separate yl, by(country)

twoway (scatter ate1 x , msymbol(S) msize(medium) mcolor(black)) ///
       (rcap yu1 yl1 x, lcolor(black))  ///
       (scatter ate2 x , msymbol(Oh) msize(large) mcolor(black)) ///
       (rcap yu2 yl2 x, lcolor(black) )  ///
       (scatter ate3 x , msymbol(T) msize(medium) mcolor(black)) ///
       (rcap yu3 yl3 x, lcolor(black) ), scheme(lean1)  ///
	   xlabel(none) xtitle(" ", size(3)) b2("Treatment groups compared to control group") ///
	   ytitle(Average treatment effect, size(3)) yscale(r(-1(.5)1)) ///
	   text(-1.1 1 "CSOs (pos.)", place(e) size(2.5)) ///
	   text(-1.1 5 "CSOs (neg.)", place(e) size(2.5)) ///
	   text(-1.1 9 "IOs (pos.)", place(e) size(2.5)) ///
	   text(-1.1 12 "IOs (neg.)", place(e) size(2.5)) ///
	   text(-1.1 15 "Governments (pos.)", place(e) size(2.5)) ///
	   text(-1.1 19.5 "Governments (neg.)", place(e) size(2.5)) ///
	   legend(order(1 "Germany" 3 "UK" 5 "US") position(1) row(1) ring(0)) ///
	   name(fig4, replace) graphregion(color(white)) ylab(, labsize(2.5) nogrid) ///
	   yline(0, lcolor(black) lpattern(dash))
		   
	   *Adjust x-title manually

 

*Fig 4.9
************************************************
use allcountries-inp-outp.dta, clear

drop yl yu ate
				
rename min95 yl
rename max95 yu
rename estimate ate
			
local varname ate
local group1 tg
local group2 country

sort `group1' `group2'
gen x = _n
replace x = _n + 1 if _n > 3
replace x = _n + 2 if _n > 6
replace x = _n + 3 if _n > 9

separate ate, by(country)
separate yu, by(country)
separate yl, by(country)

twoway (scatter ate1 x , msymbol(S) msize(medium) mcolor(black)) ///
       (rcap yu1 yl1 x, lcolor(black))  ///
       (scatter ate2 x , msymbol(Oh) msize(large) mcolor(black)) ///
       (rcap yu2 yl2 x, lcolor(black) )  ///
       (scatter ate3 x , msymbol(T) msize(medium) mcolor(black)) ///
       (rcap yu3 yl3 x, lcolor(black) ) , scheme(lean1)  ///
	   xlabel(none) xtitle(" ", size(3)) b2("Treatment groups compared to control group") ///
	   ytitle(Average treatment effect, size(3)) yscale(r(-.75(2.5).5)) ///
	   text(-1.1 .9 "Procedures (pos.)", place(e) size(2.5)) ///
	   text(-1.1 5 "Procedures (neg.)", place(e) size(2.5)) ///
	   text(-1.1 9 "Performance (pos.)", place(e) size(2.5)) ///
	   text(-1.1 13 "Performance (neg.)", place(e) size(2.5)) ///
	   legend(order(1 "Germany" 3 "UK" 5 "US") position(1) row(1) ring(0)) ///
	   name(fig5, replace) graphregion(color(white)) ylab(, labsize(2.5) nogrid) ///
	   yline(0, lcolor(black) lpattern(dash))
	  

		
				
*Fig4.10
************************************************
use allcountries-tone.dta, clear

drop yl yu ate
				
rename min95 yl
rename max95 yu
rename estimate ate
			
local varname ate
local group1 tg
local group2 country

sort `group1' `group2'
gen x = _n
replace x = _n + 1 if _n >= 1


separate ate, by(country)
separate yu, by(country)
separate yl, by(country)

twoway (scatter ate1 x , msymbol(S) msize(medium) mcolor(black)) ///
       (rcap yu1 yl1 x, lcolor(black))  ///
       (scatter ate2 x , msymbol(Oh) msize(large) mcolor(black)) ///
       (rcap yu2 yl2 x, lcolor(black) )  ///
       (scatter ate3 x , msymbol(T) msize(medium) mcolor(black)) ///
       (rcap yu3 yl3 x, lcolor(black) ) , scheme(lean1)  ///
	   xlabel(none) xtitle(" ", size(3)) b2("Treatment groups compared to control group") ///
	   ytitle(Average treatment effect, size(3)) yscale(r(-.6(.5)1)) ///
	   text(-0.7 2.5 "Positive", place(e) size(2.5)) ///
	   text(-0.7 5.5 "Negative", place(e) size(2.5)) ///
	   legend(order(1 "Germany" 3 "UK" 5 "US") position(1) row(1) ring(0)) ///
	   name(fig6, replace) graphregion(color(white)) ylab(, labsize(2.5) nogrid) ///
	   yline(0, lcolor(black) lpattern(dash))
		   
	   *Adjust x-title manually


exit

